package 经典算法;

import java.util.Arrays;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: 冯若航
 * \* Date: 2021/5/12
 * \* Time: 20:29
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
public class 快速排序 {
    public static void QuickSort(int[] nums,int left,int right){
        if(left<right){
            int pivot=Partition(nums,left,right);
            QuickSort(nums, left, pivot-1);
            QuickSort(nums, pivot+1, right);
        }
    }

    public static int Partition(int[] nums,int left,int right){
        int pivot=nums[left];
        while (left<right){
            while(left<right&&pivot<nums[right]) {right--;}
            nums[left]=nums[right];
            while (left<right&&pivot>nums[left]) {left++;}
            nums[right]=nums[left];
        }

        nums[left]=pivot;
        return left;
    }
    public static void main(String[] args) {
        int[] test=new int[]{5,3,6,8,7};
        QuickSort(test,0,4);
        System.out.println(Arrays.toString(test));
    }
}
